package com.zjf.carpark.controller;


import com.zjf.carpark.annotation.MyLogAnno;
import com.zjf.carpark.entities.ParkPlace;
import com.zjf.carpark.service.impl.ParkPlaceServiceImpl;
import com.zjf.carpark.utils.ResponseData;
import com.zjf.carpark.vo.ParkPlaceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.zjf.carpark.utils.CodeAndMessage.*;

/**
 * @description: 停车位
 * @author: 张建飞
 * @createDate: 2019/12/27
 * @version: 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("parkPlace")
public class ParkPlaceController {

    @Autowired
    ParkPlaceServiceImpl parkPlaceService;

    ResponseData responseData;


    /**
     * 新增停车位
     *
     * @param parkPlace
     * @return
     */
    @RequestMapping("add")
    @MyLogAnno("新增停车位")
    public ResponseData add(ParkPlace parkPlace) {
        Integer save = parkPlaceService.save(parkPlace);
        if (save != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }

    /**
     * 通过车位编号查询是否存在车位
     *
     * @param parkPlaceNum
     * @return
     */
    @RequestMapping("selectByNum")
    public ResponseData selectByNum(String parkPlaceNum) {
        int i = parkPlaceService.selectByNum(parkPlaceNum);
        if (i != 0) {
            responseData = new ResponseData(CODE200, SUCCESS);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }

    /**
     * 分页查询所有
     *
     * @param parkPlaceVo
     * @return
     */
    @RequestMapping("selectAll")
    public ResponseData selectAll(ParkPlaceVO parkPlaceVo) {
        List<ParkPlace> parkPlaces = new ArrayList<>();
        int totalCount = 0;
        if ("普通车位".equals(parkPlaceVo.getDesc())) {
            parkPlaces = parkPlaceService.selectNormalByMsg(parkPlaceVo);
            totalCount = parkPlaceService.totalCountNormal(parkPlaceVo);
        } else {
            parkPlaces = parkPlaceService.selectByMsg(parkPlaceVo);
            totalCount = parkPlaceService.totalCount(parkPlaceVo);
        }

        if (parkPlaces != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("parkPlaces", parkPlaces);
            responseData.setCount(totalCount);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }

    /**
     * 查找所有的固定车位
     *
     * @return
     */
    @RequestMapping("selectAllFixed")
    public ResponseData selectAllFixed() {
        List<ParkPlace> parkPlaces = parkPlaceService.selectAllFixed();
        List<String> parkPlaceNum = new ArrayList<>();
        List<Integer> parkPlaceIds = new ArrayList<>();
        if (parkPlaces != null) {
            for (ParkPlace parkPlace : parkPlaces) {
                parkPlaceNum.add(parkPlace.getParkPlaceNum());
                parkPlaceIds.add(parkPlace.getParkPlaceId());
            }
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("parkPlaceNum", parkPlaceNum);
            responseData.putDataValue("parkPlaceId", parkPlaceIds);
        } else {
            responseData = new ResponseData(CODE0, ERROR);
        }
        return responseData;
    }


    /**
     * 剩余的空的普通车位
     *
     * @return
     */
    @RequestMapping("emptyCount")
    public ResponseData emptyCount() {
        int count = parkPlaceService.emptyCount();
        if (count >= 0) {
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("emptyParkPlace", count);
        }
        return responseData;
    }

    /**
     * 查找所有车位用于前端进行视图渲染
     *
     * @return
     */
    @RequestMapping("selectAllParkPlace")
    public ResponseData selectAllParkPlace() {
        List<ParkPlace> parkPlaces = parkPlaceService.selectAllParkPlace();
        responseData = new ResponseData(CODE200, SUCCESS);
        responseData.putDataValue("parkPlaces", parkPlaces);
        return responseData;
    }
}
